//
//  LQLRTableView.m
//  PilotSeries
//
//  Created by 黄立强 on 16/4/18.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "LQLRTableView.h"
#import "LQLeftCell.h"
#import "LQCenterCell.h"
#import "LQRightCell.h"
#import "PAMenu.h"


@interface  LQLRTableView()<UITableViewDataSource, UITableViewDelegate>
/** 子数据 */
@property (nonatomic, strong) NSArray *centerdata;
/** 记录左边选中的哪一行 */


@property (nonatomic, strong) NSArray *rightdata;

@end
@implementation LQLRTableView


+ (instancetype)lrTableView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"LQLRTableView" owner:nil options:nil] firstObject];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) { // 左边的tableView
        return [self.dataSource numberOfRowsInLeftTableViewWithLQLRTableView:self];
    } else if (tableView == self.centerTableView){ // 右边的tableView
        return self.centerdata.count;
    }else if (tableView == self.rightTableView) {
        return self.rightdata.count;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (tableView == self.leftTableView) { // 左边的tableView
        // 0.创建Cell
        cell = [LQLeftCell leftCellWithTableView:tableView];
        
        // 1.给cell设置数据
        cell.textLabel.text = [self.dataSource lrTableView:self leftTitleInRow:indexPath.row];
        
        if ([self.dataSource lrTableView:self submenusForRow:indexPath.row]) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
        BOOL checked  =  [self.dataSource lrTableView:self leftCheckedforRow:indexPath.row ];
        if (checked) {
            cell.textLabel.textColor = [UIColor redColor];
             cell.imageView.image = [UIImage imageNamed:@"checked"];
        }else{
            cell.textLabel.textColor = [UIColor blueColor];
            cell.imageView.image = [UIImage imageNamed:@"mainCellBackground"];
        }
        
        BOOL enabled =  [self.dataSource lrTableView:self leftenabledforRow:indexPath.row];
        cell.textLabel.enabled = enabled;

    }else if (tableView == self.centerTableView) {
        cell  = [LQCenterCell centerCellWithTableView:tableView];
        PAMenu *menu = [self.centerdata objectAtIndex:indexPath.row];
        //cell.textLabel.text=  [NSString stringWithFormat:@"%@->%@",menu.label,menu.eventId];
        cell.textLabel.text = menu.label;
        if (menu.submenu != nil ) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        BOOL checked  =  menu.checked == 1;
        if (checked) {
            cell.textLabel.textColor = [UIColor redColor];
            cell.imageView.image = [UIImage imageNamed:@"checked"];
        }else{
            cell.textLabel.textColor = [UIColor blueColor];
            cell.imageView.image = [UIImage imageNamed:@"mainCellBackground"];
            
        }
        BOOL enabled = menu.enabled== 1;
        cell.textLabel.enabled = enabled;
        
    }else { // 右边的tableView
        // 0.创建Cell
        cell = [LQRightCell rightCellWithTableView:tableView];
        PAMenu *menu = [self.rightdata objectAtIndex:indexPath.row];
        cell.textLabel.text = menu.label;
        //cell.textLabel.text=  [NSString stringWithFormat:@"%@->%@",menu.label,menu.eventId];
        if (menu.submenu != nil ) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        BOOL checked  =  menu.checked == 1;
        if (checked) {
            cell.textLabel.textColor = [UIColor redColor];
            cell.imageView.image = [UIImage imageNamed:@"checked"];
        }else{
            cell.textLabel.textColor = [UIColor blueColor];
            cell.imageView.image = [UIImage imageNamed:@"mainCellBackground"];
            
        }
        BOOL enabled = menu.enabled== 1;
        cell.textLabel.enabled = enabled;
    }
    
    return cell;
}

#pragma mark - 实现tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) { // 左边的tableView
        
        // 1.获取子数据
        self.centerdata = [self.dataSource lrTableView:self centerDataOfRow:indexPath.row];
        
        // 2.刷新右边表格
        [self.centerTableView reloadData];
        
        self.rightdata  = @[];
        [self.rightTableView reloadData];
        // 3.通知代理左边发生了点击
        if ([self.delegate respondsToSelector:@selector(lrTableView:selectedLeftIndex:)]) {
            [self.delegate lrTableView:self selectedLeftIndex:indexPath.row];
        }
        
        
        // 4.记录左边选中哪一行
        self.currentLeftIndex = indexPath.row;
    } else if(tableView == self.centerTableView) {
        self.rightdata = [self.dataSource lrTableView:self rightDataOfRow:indexPath.row];
        [self.rightTableView reloadData];
        [self.delegate lrTableView:self selectedLeftIndex:self.currentLeftIndex andCenterIndex:indexPath.row];
        self.currentCenterIndex = indexPath.row;
    } else { // 右边的tableView
//        if ([self.delegate respondsToSelector:@selector(lrTableView:selectedLeftIndex:andRightIndex:)]) {
//            //[self.delegate lrTableView:self selectedLeftIndex:self.currentLeftIndex andRightIndex:indexPath.row];
//        }
        [self.delegate lrTableView:self selectedLeftIndex:self.currentLeftIndex andCenterIndex:self.currentCenterIndex andRightIndex:indexPath.row];
    }
}

-(void) reoadData{
    
    [self.leftTableView reloadData];
    [self.centerTableView reloadData];
    [self.rightTableView reloadData];
}

@end
